package cn.bs.busuo.mall.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.common.pojo.vo.PageData;
import cn.bs.busuo.mall.dao.persist.repository.ICommentRepository;
import cn.bs.busuo.mall.dao.persist.repository.IGoodsRepository;
import cn.bs.busuo.mall.pojo.entity.Comment;
import cn.bs.busuo.mall.pojo.entity.Goods;
import cn.bs.busuo.mall.pojo.vo.CommentAddNewParam;
import cn.bs.busuo.mall.pojo.vo.CommentListItemVO;
import cn.bs.busuo.mall.service.ICommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements ICommentService {
    @Value("${busuo.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICommentRepository commentRepository;
    @Autowired
    private IGoodsRepository goodsRepository;

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, CommentAddNewParam commentAddNewParam) {
        Long goodsId = commentAddNewParam.getGoodsId();
        Goods currentGoods = goodsRepository.getStandardById(goodsId);
        if (currentGoods == null) {
            String message = "发表评论失败，商品数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentGoods.getIsPutOn() == 0) {
            String message = "发表评论失败，此商品已经下架！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam, comment);
        comment.setAuthorId(currentPrincipal.getId());
        comment.setAuthorName(currentPrincipal.getUsername());
        comment.setCheckState(0);
        int rows = commentRepository.insert(comment);
        if (rows != 1) {
            String message = "发表评论失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        Goods updateGoods = new Goods();
        updateGoods.setId(goodsId);
        updateGoods.setCommentCount(currentGoods.getCommentCount() + 1);
        switch (commentAddNewParam.getType()) {
            case 0:
                updateGoods.setPositiveCommentCount(currentGoods.getPositiveCommentCount() + 1);
                break;
            case 2:
                updateGoods.setNegativeCommentCount(currentGoods.getNegativeCommentCount() + 1);
                break;
        }
        rows = goodsRepository.updateById(updateGoods);
        if (rows != 1) {
            String message = "发表评论失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageData<CommentListItemVO> list(Long goodsId, Integer pageNum){

        return commentRepository.list(goodsId, pageNum, defaultQueryPageSize);
    }
}
